Messages support several operations, as listed in Table 1.
Table 1. Messages Operations
Operation | Description |
---|
Put Message | En-queues a message at the end of the specified queue. The message can't be more than 8KB in size. |
Get Messages | Dequeues
one or more messages from the front of the specified queue. The maximum
number of messages that can be retrieved in a single call is 32. The
messages received are marked invisible until the visibilitytimeout
property of the message expires. The default visibilitytimeout is 30
seconds, and the maximum value is 2 hours. |
Peek Messages | Reads
one or more messages from the front of the specified queue. This method
doesn't alter the visibilitytimeout property of a message, so the
messages are visible to other applications at the same time. |
Delete Message | Deletes
the specified message from the queue. The Queue service marks the
message for deletion, and the message is deleted during the next
garbage-collection cycle. The delete operation requires the MessageId
and PopReceipt of the message to be passed to the Queue service. |
Clear Messages | Deletes all messages from the specified queue. |
Table 2 lists some of the important characteristics of the message operations.
The <account name> is the storage account name in the cloud, and the <devstorageaccount> is the development storage account. The <queue name> is the name of the queue in which messages are stored. The following sections discuss some of the operations from Table 5-9
in detail. Even though the operations are different, the programming
concepts behind them are similar. To keep the book at a conceptual
level, I discuss just the Put Message and Get Messages operations. By
studying these two operations in detail, you can understand the
programming concepts behind all the message operations. The Windows
Azure Storage Operations application included with this chapter's
source code contains implementations of most of the message operations.
1. Put Message
The Put Message operation
en-queues (puts) a message at the end of the queue. The URI of a Put
Message operation is of the format account name>.queue.core.windows.net/<queue name>/messages.
You can send a message with size up to 8KB. To send larger files, you
can save the message as a blob and send the URI of the blob to the
queue. The body of the message while sending can be text or binary, but
it should support inclusion in an XML body with UTF-8 encoding. This is
because a message received from the queue is always returned in
Base64-encoded format within an XML response body. You see this in the
Get Messages operation. The URI for the Put Message operation supports
an additional optional parameter, listed in Table 3.
Table 3. Put Message URI Parameter
Parameter | Description | Example |
---|
messagettl | This
is an integer value of seconds representing the time-to-live for a
message in the queue before it's retrieved or deleted. The default and
the maximum value for messagettl is 7 days, after which the message is
garbage-collected. | account name>.queue.core.windows.net/<queuename>/messages?messagettl=60 |
The Put Message REST request looks like Listing 1.
Example 1. Put Message REST Request
POST /myfirstazurequeue/messages?messagettl=120&timeout=30 HTTP/1.1 x-ms-date: Thu, 18 Jun 2009 05:52:00 GMT Authorization: SharedKey proazurestorage:Ahv5yhR9xOrHiMTnq3fBcaBKL8KeUFQ3r Host: proazurestorage.queue.core.windows.net Content-Length: 84 Expect: 100-continue
<QueueMessage> <MessageText>bXlmaXJzdGF6dXJlbWVzc2FnZQ==</MessageText> </QueueMessage>
|
In Listing 1,
a string message "myfirstazuremessage" is sent to the queue named
myfirstazurequeue. The time-to-live seconds for the message is 120,
which means if the message isn't received or deleted by an application
within 120 seconds in the queue, the message will be marked for
deletion and won't be visible to any applications. The request body
consists of the message content wrapped in the <QueueMessage>
element. Note that the content of the message within the
<MessageText /> element is in Base64-encoded format. Listing 2 shows the response from the Queue service.
Example 2. Put Message REST Response
HTTP/1.1 201 Created Server: Queue Service Version 1.0 Microsoft-HTTPAPI/2.0 x-ms-request-id: e724cc82-3d21-4253-9317-3b3964374be7 Date: Thu, 18 Jun 2009 05:53:32 GMT Content-Length: 0
|
As shown in Listing 2, the Queue service responds with an HTTP/1.1 201 Created status code for a successful Put Message operation. Figure 1 shows the working of the Put Message operation in the Windows Azure Storage Operations application.
As illustrated in Figure 1,
you can send a text message using the Windows Azure Storage Operations
application. The steps for sending a message to a queue are as follows:
Create a new queue (called myfirstazurequeue).
Select the new queue from the Queues List Box in the Accounts section.
Add some text to the Message Body text box in the Queues section.
Select the Put Message operation from the Operations text box.
Make sure the Queue Name text box is populated with the selected queue name.
Optionally, you can specify the time-to-live in the "Time to live (secs)" text box.
Click the Execute button to execute the Put Message operation.
The WindowsAzureStorage.cs file in the Windows Azure
Storage Operations project consists of a PutMessage() method, as shown
in Listing 3.
Example 3. PutMessage() Method in WindowsAzureStorage.cs
private void PutMessage() { string messageBody = string.Empty; if (txtMessageBody.Text.Length > 0) { messageBody = txtMessageBody.Text;
} else { messageBody = String.Format ("Message from Windows Azure Storage Operations", System.Guid.NewGuid().ToString("N")); } int ttlsecs = 300; if(txtTimeTolive.Text.Length > 0) { ttlsecs = int.Parse(txtTimeTolive.Text); } if (StorageHelper.PutMessage(txtQueueName.Text, new Microsoft.Samples.ServiceHosting.StorageClient.Message(messageBody), ttlsecs)) { statusLabel.Text = String.Format ("Message {0} sent successfully to queue {1}", messageBody, txtQueueName.Text);
} }
|
As shown in Listing 3,
the PutMessage() method calls the PutMessage() operation on the
WindowsAzureStorageHelper (StorageHelper object). The message body and
the time-to-live values are parsed appropriately from the server
controls and passed as parameters to the method. If you don't enter a
message in the Message Body text box, the application automatically
creates a default message for you. Figure 2
shows the sequence of Put Message operation from the application to the
Queue service as it passes through various assemblies and objects.
As shown in Figure 2,
the Windows Azure Storage Operations application calls the PutMessage()
method on the WindowsStorageHelper object in the ProAzureCommonLib.dll.
The WindowsStorageHelper object calls the GetQueue() method on the
QueueStorage object to get an instance of the MessageQueue object. The
WindowsStorageHelper object then calls the PutMessage() method on the
MessageQueue object. The PutMessage() method of the MessageQueue object
creates the REST message request and sends it synchronously to the
Queue service. It uses the System.Net.HttpWebRequest to send the REST
message over HTTP. Upon the success of the operation, the Queue service
returns an HTTP status code: HTTP/1.1 201 Created. The PutMessage()
method translates the HTTP code into true for success and false for
failure. The Boolean value is passed all the way to the Windows Azure
Storage Operations application as a return parameter of the
PutMessage() method.